package com.anxin.registerManagement.controller;

import com.anxin.registerManagement.bean.jo.DormitorySearchJo;
import com.anxin.registerManagement.bean.persist.Dormitory;
import com.anxin.registerManagement.service.DormitoryService;
import com.anxin.registerManagement.util.Assert;
import com.anxin.registerManagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 叶前呈
 * @date 2022/1/29 15:55
 */
@Api(tags = "宿舍接口")
@RestController
@RequestMapping("/dormitories")
public class DormitoryController {

    private static final Logger logger = LoggerFactory.getLogger(DormitoryController.class);

    @Autowired
    DormitoryService dormitoryService;

    @ApiOperation("查询宿舍")
    @PostMapping("/search")
    public ResultUtil getDormitoryList(@RequestBody DormitorySearchJo dormitorySearchJo) {
        logger.info("查询宿舍：{}", dormitorySearchJo);
        return ResultUtil.success(dormitoryService.getDormitoryList(dormitorySearchJo));
    }

    @ApiOperation("添加宿舍")
    @PostMapping("/add")
    public ResultUtil addDormitory(@Valid @RequestBody Dormitory dormitory) {
        logger.info("添加宿舍：{}", dormitory);
        dormitoryService.addDormitory(dormitory);
        return ResultUtil.success();
    }

    @ApiOperation("修改宿舍")
    @PostMapping("/update")
    public ResultUtil updateDormitory(@Valid @RequestBody Dormitory dormitory) {
        Assert.notNull(dormitory.getId(), "宿舍id不能为空");
        logger.info("修改宿舍：{}", dormitory);
        dormitoryService.updateDormitory(dormitory);
        return ResultUtil.success();
    }

    @ApiOperation("删除宿舍")
    @PostMapping("/delete")
    public ResultUtil deleteDormitory(@RequestParam("id") int id) {
        Assert.notNull(id, "宿舍id不能为空");
        logger.info("删除宿舍id：{}", id);
        dormitoryService.deleteDormitory(id);
        return ResultUtil.success();
    }

    @ApiOperation("获取所有宿舍")
    @GetMapping("/get")
    public ResultUtil getAllDormitory() {
        return ResultUtil.success(dormitoryService.getAllDormitory());
    }

}
